//npm install imagejs
//https://www.npmjs.com/package/imagejs#crop
//https://help.apple.com/app-store-connect/#/devd274dd925


https://www.cnblogs.com/yujidewu/p/7803675.html
var ImageJS = require("imagejs");

function calc_px(a,b){
	return Math.abs(parseInt((a-b)/2));
}

var filename='./src/splash.png';
var width=2732;
var height=2732;


var bitmap = new ImageJS.Bitmap();
bitmap.readFile(filename)
    .then(function() {
        // bitmap is ready
		
	// var thumbnail = bitmap.resize({
		// width: 64, height: 64,
		// algorithm: "nearestNeighbor"
	// });
	
	// // write to a jpg file, quality 75 (default is 90)
	// return thumbnail.writeFile("image.jpg", { quality:75 })
		// .then(function() {
			// // bitmap has been saved
		// });
		var boxes=[
			[480,762],
			[720,1242],
			[1080,1882],
		];
		for(let i=0;i<boxes.length;i++){
			let w,h,target_file;
			// 480*762
			w=boxes[i][0];
			h=boxes[i][1];
            var index=i+1;
			target_file='./dst/android/'+index+'-'+w+'@'+h+'.png';
			bitmap.crop({top: calc_px(height,h), left: calc_px(width,w), width: w, height: h})
			.writeFile(target_file, { quality:100 })
			.then(function() {
				// bitmap has been saved
				console.log(w+'x'+h,'success!');
			});
		}
		
		var boxes2=[
            [1242,2688],
            [828,1792],
            [1125,2436],
            [1242,2208],
            [750,1334],
            [640,1136],
            [640,960],

		];
		
		for(let i=0;i<boxes2.length;i++){
			let w,h,target_file;
			// 480*762
			w=boxes2[i][0];
			h=boxes2[i][1];
            var index=i+1;
			target_file='./dst/iphone/'+index+'-'+w+'@'+h+'.png';
			bitmap.crop({top: calc_px(height,h), left: calc_px(width,w), width: w, height: h})
			.writeFile(target_file, { quality:100 })
			.then(function() {
				// bitmap has been saved
				console.log(w+'x'+h,'success!');
			});
		}
        
        var boxes3=[
            [2018,2732],
            [1668,2388],
            [1668,2224],
            [1536,2048],
            [768,1024],
			
            
            
            
		];
		
		for(let i=0;i<boxes3.length;i++){
			let w,h,target_file;
			// 480*762
			w=boxes3[i][0];
			h=boxes3[i][1];
            var index=i+1;
			target_file='./dst/ipad/'+index+'-'+w+'@'+h+'.png';
			bitmap.crop({top: calc_px(height,h), left: calc_px(width,w), width: w, height: h})
			.writeFile(target_file, { quality:100 })
			.then(function() {
				// bitmap has been saved
				console.log(w+'x'+h,'success!');
			});
		}
		
		
	
		
		
    });
	
